package com.ysh.smartplatform.event.listener;

import com.ysh.smartplatform.event.DeviceStatusEvent;
import com.ysh.smartplatform.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 袁胜虎 on 2022/9/13 17:35
 */
@Component
public class DeviceStatusListener {
    private DeviceService deviceService;

    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }
    @Async
    @EventListener
    public void onStatusChange(DeviceStatusEvent event){
        deviceService.updateDeviceStatus(event.getDeviceId(),event.getStatus() == DeviceStatusEvent.Status.OFFLINE ? 0 : 1);
    }

}
